package com.baifc.wiringbean.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baifc.wiringbean.soundsystem.CDPlayer;
import com.baifc.wiringbean.soundsystem.CompactDisc;
import com.baifc.wiringbean.soundsystem.SgtPeppers;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;

/**
 * projectName: springinaction
 * packageName: soundsystem
 * Created: 2019/4/22.
 * Auther: baifc
 * Description:
 */
@Configuration
//@ComponentScan(basePackages = "com.baifc.wiringbean.soundsystem")  // 自动扫描所在包中需要被创建的bean
@Import(ExpressiveConfig.class)
public class CDPlayerConfig {

    @Autowired
    private Environment environment;

//    @Bean
//    @Profile("pro")
//    public CompactDisc sgtPeppers(@Value("${disc.title}") String title, @Value("${disc.artist}") String artist) {
//        return new SgtPeppers(title, artist);
//    }

    @Bean
    @Profile("pro")
    public CompactDisc sgtPeppers() {
        return new SgtPeppers();
    }

    @Bean
    public PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

//    @Bean
//    @Profile("pro")
//    public CompactDisc sgtPeppers() {
//        SgtPeppers sgtPeppers = new SgtPeppers();
//        if (environment.containsProperty("disc.artist")) {
//            sgtPeppers.setArtist(environment.getProperty("disc.artist"));
//        }
//        if (environment.containsProperty("disc.title")) {
//            sgtPeppers.setTitle(environment.getProperty("disc.title"));
//        }
//
//        return sgtPeppers;
//    }

//    @Bean
//    public CDPlayer cdPlayer() {
//        // 这里调用sgtPeppers并不会对方法进行实际调用，因为SgtPepper方法上添加了bean注解，
//        // spring会拦截所有对它的调用，并直接返回该方法所创建的实例，spring中的bean默认都是单例的
//        return new CDPlayer(sgtPeppers());
//    }



    @Bean
    public CDPlayer cdPlayer(CompactDisc compactDisc) {
        // 该方法也可以请求一个参数，spring会将已创建好的bean自动装配到方法中
        return new CDPlayer(compactDisc);
    }
}
